package com.hospital.workstation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.hospital.common.utils.StringUtils;
import com.hospital.common.core.page.TableDataInfo;
import com.hospital.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.hospital.workstation.domain.bo.HisMedicineStorageBo;
import com.hospital.workstation.domain.vo.HisMedicineStorageVo;
import com.hospital.workstation.domain.HisMedicineStorage;
import com.hospital.workstation.mapper.HisMedicineStorageMapper;
import com.hospital.workstation.service.IHisMedicineStorageService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 药品库存Service业务层处理
 *
 * @author whb
 * @date 2023-03-10
 */
@RequiredArgsConstructor
@Service
public class HisMedicineStorageServiceImpl implements IHisMedicineStorageService {

    private final HisMedicineStorageMapper baseMapper;

    /**
     * 查询药品库存
     */
    @Override
    public HisMedicineStorageVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询药品库存列表
     */
    @Override
    public TableDataInfo<HisMedicineStorageVo> queryPageList(HisMedicineStorageBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HisMedicineStorage> lqw = buildQueryWrapper(bo);
        Page<HisMedicineStorageVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询药品库存列表
     */
    @Override
    public List<HisMedicineStorageVo> queryList(HisMedicineStorageBo bo) {
        LambdaQueryWrapper<HisMedicineStorage> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HisMedicineStorage> buildQueryWrapper(HisMedicineStorageBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HisMedicineStorage> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getMedicineName()), HisMedicineStorage::getMedicineName, bo.getMedicineName());
        lqw.like(StringUtils.isNotBlank(bo.getMedicineHouse()), HisMedicineStorage::getMedicineHouse, bo.getMedicineHouse());
        lqw.eq(StringUtils.isNotBlank(bo.getMedicalInsuranceDesc()), HisMedicineStorage::getMedicalInsuranceDesc, bo.getMedicalInsuranceDesc());
        lqw.eq(StringUtils.isNotBlank(bo.getIsBaseMedicine()), HisMedicineStorage::getIsBaseMedicine, bo.getIsBaseMedicine());
        lqw.eq(StringUtils.isNotBlank(bo.getPurchaseBatch()), HisMedicineStorage::getPurchaseBatch, bo.getPurchaseBatch());
        lqw.eq(StringUtils.isNotBlank(bo.getMedicineSpec()), HisMedicineStorage::getMedicineSpec, bo.getMedicineSpec());
        lqw.eq(StringUtils.isNotBlank(bo.getMeterUnit()), HisMedicineStorage::getMeterUnit, bo.getMeterUnit());
        lqw.eq(StringUtils.isNotBlank(bo.getProductFactory()), HisMedicineStorage::getProductFactory, bo.getProductFactory());
        lqw.eq(StringUtils.isNotBlank(bo.getMedicinePrice()), HisMedicineStorage::getMedicinePrice, bo.getMedicinePrice());
        lqw.eq(StringUtils.isNotBlank(bo.getMedicineAmount()), HisMedicineStorage::getMedicineAmount, bo.getMedicineAmount());
        lqw.eq(StringUtils.isNotBlank(bo.getMedicineFirstSpell()), HisMedicineStorage::getMedicineFirstSpell, bo.getMedicineFirstSpell());
        lqw.isNotNull(HisMedicineStorage::getMedicalCode);
        return lqw;
    }

    /**
     * 新增药品库存
     */
    @Override
    public Boolean insertByBo(HisMedicineStorageBo bo) {
        HisMedicineStorage add = BeanUtil.toBean(bo, HisMedicineStorage.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改药品库存
     */
    @Override
    public Boolean updateByBo(HisMedicineStorageBo bo) {
        HisMedicineStorage update = BeanUtil.toBean(bo, HisMedicineStorage.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HisMedicineStorage entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除药品库存
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 根据搜索值查询药品列表
     *
     * @param searchValue 搜索值
     * @return
     */
    @Override
    public List<HisMedicineStorageVo> getStorageListBySearch(String searchValue) {
        LambdaQueryWrapper<HisMedicineStorage> lqw = new LambdaQueryWrapper<>();
        lqw.like(HisMedicineStorage::getMedicineName, searchValue)
            .or()
            .like(HisMedicineStorage::getMedicineFirstSpell, searchValue)
            .isNotNull(HisMedicineStorage::getMedicalCode)
            .select(HisMedicineStorage::getMedicineName);
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 批量新增或修改药品库存
     *
     * @param storageList 药品库存List
     * @return
     */
    @Override
    public Boolean insertOrUpdateBatch(List<HisMedicineStorage> storageList) {
        return baseMapper.insertOrUpdateBatch(storageList);
    }
}
